<?php
class Hopdong_AdminPhongbanController extends Zendvn_Controller_Action{
	
	protected $_arrParam; //Mang tham so nhan duoc khi mot Action chay
	protected $_currentController; //Duong dan cua Controller
	protected $_actionMain; //Duong dan cua Action chinh
	protected $_namespace;
	protected $_page = ''; //Url page
	
	public function init(){
		$this->_arrParam = $this->_request->getParams(); //Mang tham so nhan duoc khi mot Action chay
		$this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller']; //Duong dan cua Controller
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller'] . '/index'; //Duong dan cua Action chinh
	
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		$this->_arrParam['ssFilter']['keywords'] 	= $ssFilter->keywords;
		$this->_arrParam['ssFilter']['parents'] 	= $ssFilter->parents;
		
		//Thanh vien dang login
		$info 			= new Zendvn_System_Info();
		$this->_arrParam['adminInfo'] 	= $info->getMemberInfo();

		//Truyen ra ngoai view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	
		$this->view->siteConfig = Zend_Registry::get('siteConfig');
		$template_path = TEMPLATE_PATH . "/admin/" . $this->view->siteConfig['template']['admin'];
		$this->loadTemplate($template_path, 'template.ini', 'template');
	}

	public function indexAction(){
		$this->view->Title = 'Phòng ban :: Danh sách';
		$this->view->headTitle($this->view->Title, true);
		$tblPhongban = new Hopdong_Model_Phongban();
		$this->view->slbCategory = $tblPhongban->itemInSelectbox($this->_arrParam);
		
		$this->view->Items = $tblPhongban->listItem($this->_arrParam, array('task'=>'admin-list'));
	}
	
	public function filterAction(){
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		
		if($this->_arrParam['type'] == 'search'){
			if($this->_arrParam['key'] == 1){
				$ssFilter->keywords = trim($this->_arrParam['keywords']);
			}else{
				$ssFilter->keywords = '';
			}
		}
		
		if($this->_arrParam['type'] == 'category'){
			$ssFilter->parents = $this->_arrParam['parents'];
		}
	
		$this->_redirect($this->_actionMain);
	
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function addAction(){
		$this->view->Title = 'Phòng ban :: Thêm mới';
		$this->view->headTitle($this->view->Title, true);
		$tblPhongban = new Hopdong_Model_Phongban();
		$this->view->slbCategory = $tblPhongban->itemInSelectbox($this->_arrParam);
		
		if($this->_request->isPost()){
			$validator = new Default_Form_ValidatePhongban($this->_arrParam);
			if($validator->isError() == true){
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();
			}else{
				$tblPhongban->saveItem($this->_arrParam,array('task'=>'admin-add'));
				$this->_redirect($this->_actionMain . '/save/ok');
			}
		}
	}
	
	public function infoAction(){
		$this->view->Title = 'Phòng ban :: Xem thông tin';
		$this->view->headTitle($this->view->Title, true);
		$tblPhongban = new Hopdong_Model_Phongban();
		$this->view->Item = $tblPhongban->getItem($this->_arrParam,array('task'=>'admin-info'));
		
		if($this->view->Item['parents'] > 0){
			$this->view->Parents = $tblPhongban->getItem(array('id' => $this->view->Item['parents']), array('task' => 'admin-info'));
		}
		
		$tblUser = new Default_Model_Users();
		if(!empty($this->view->Item['created_by'])){
			$this->view->createdBy = $tblUser->getItem(array('id' => $this->view->Item['created_by']),array('task'=>'admin-info'));
		}
		if(!empty($this->view->Item['modified_by'])){
			$this->view->modifiedBy = $tblUser->getItem(array('id' => $this->view->Item['modified_by']),array('task'=>'admin-info'));
		}
	}
	
	public function editAction(){
		$this->view->Title = 'Phòng ban :: Sửa';
		$this->view->headTitle($this->view->Title, true);
		$tblPhongban = new Hopdong_Model_Phongban();
		$this->view->Item = $tblPhongban->getItem($this->_arrParam,array('task'=>'admin-edit'));
		$this->view->slbCategory = $tblPhongban->itemInSelectbox($this->_arrParam,array('task'=>'admin-edit'));
		
		if($this->_request->isPost()){
			$validator = new Default_Form_ValidatePhongban($this->_arrParam);
			if($validator->isError() == true){
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();
			}else{
				$tblPhongban->saveItem($this->_arrParam,array('task'=>'admin-edit'));
				$this->_redirect($this->_actionMain . '/save/ok');
			}
		}
	}
	
	public function deleteAction(){
		$this->view->Title = 'Phòng ban :: Xóa';
		$this->view->headTitle($this->view->Title, true);
		if($this->_request->isPost()){
			if(($this->_arrParam['type'] == 'multi-delete') && ($this->_arrParam['task'] == 'ok')){
				$tblPhongban = new Hopdong_Model_Phongban();
				$tblPhongban->deleteItem($this->_arrParam, array('task'=>'admin-delete-muti'));
				$this->_redirect($this->_actionMain . '/save/ok');
			}else
				if(!empty($this->_arrParam['id'])){
				$tblPhongban = new Hopdong_Model_Phongban();
				$tblPhongban->deleteItem($this->_arrParam, array('task'=>'admin-delete'));
				$this->_redirect($this->_actionMain . '/save/ok');
			}
		}
	}
	
	public function statusAction(){
		$tblPhongban = new Hopdong_Model_Phongban();
		$tblPhongban->changeStatus($this->_arrParam);
		$this->_redirect($this->_actionMain . '/save/ok');
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function sortAction(){
		if($this->_request->isPost()){
			$tblPhongban = new Hopdong_Model_Phongban();
			$tblPhongban->sortItem($this->_arrParam, array('task'=>'admin-sort'));
			$this->_redirect($this->_actionMain . '/save/ok');
		}
		$this->_helper->viewRenderer->setNoRender();
	}

}



